from PyQt5.QtCore import Qt, QSize, QPoint, QTimer
from PyQt5.QtGui import QPainter, QPen, QResizeEvent
from PyQt5.QtWidgets import QApplication, QWidget
from utils.maze_generator import *
import sys


class M_G(QWidget):
    def __init__(self, size):
        super(M_G, self).__init__()
        self.size = size
        self.maze, self.enterance, self.exit = generate_maze(self.size, self.size)
        self.resize(1000, 1000)
        self.f = 1000 // len(self.maze)
        self.pen_set()
        self.showMap(self.maze)

    def showMap(self, maze):
        y = len(maze)
        x = len(maze[0])
        for j in range(y):
            for i in range(x):
                print(maze[j][i], end=" ")
            print('\n')

    def pen_set(self, width=6, color=Qt.darkBlue):
        self.pen1 = QPen()
        self.pen1.setJoinStyle(Qt.RoundJoin)
        self.pen1.setCapStyle(Qt.RoundCap)
        self.pen1.setWidth(6)
        self.pen1.setColor(color)
        self.pen2 = QPen()
        self.pen2.setJoinStyle(Qt.RoundJoin)
        self.pen2.setCapStyle(Qt.RoundCap)
        self.pen2.setWidth(12)
        self.pen2.setColor(Qt.red)
        self.pen3 = QPen()
        self.pen3.setJoinStyle(Qt.RoundJoin)
        self.pen3.setCapStyle(Qt.RoundCap)
        self.pen3.setWidth(6)
        self.pen3.setColor(Qt.gray)



    def paintEvent(self, QPaintEvent):
        painter = QPainter(self)
        painter.translate(10, 10)
        painter.setPen(self.pen1)

        visited = list()
        def paint(coordinate):
            y, x = coordinate
            if self.__is_path__(coordinate):
                if coordinate in visited:
                    return
            visited.append(coordinate)
            neighbor_coordinates = [[y,x+1],[y,x-1],[y-1,x],[y+1,x]]
            for nc in neighbor_coordinates:
                if nc not in visited and self.__is_path__(nc):
                    painter.drawLine(coordinate[1]*self.f,coordinate[0]*self.f,nc[1]*self.f,nc[0]*self.f)
                    paint(nc)
        paint(self.enterance)

        visited = list()
        def paint(coordinate):
            y, x = coordinate
            if not self.__is_wall__(coordinate):
                if coordinate in visited:
                    return
            visited.append(coordinate)
            neighbor_coordinates = [[y, x + 1], [y, x - 1], [y - 1, x], [y + 1, x]]
            for nc in neighbor_coordinates:
                if nc not in visited and self.__is_wall__(nc):
                    painter.drawLine(coordinate[1] * self.f, coordinate[0] * self.f, nc[1] * self.f, nc[0] * self.f)
                    paint(nc)

        painter.setPen(self.pen2)
        painter.drawPoint(self.enterance[1]*self.f,self.enterance[0]*self.f)
        painter.drawPoint(self.exit[1] * self.f, self.exit[0] * self.f)
        painter.setPen(self.pen3)
        paint([0,0])
        paint([len(self.maze)-1, 0])
        paint([len(self.maze) - 1, len(self.maze[0])-1])
        paint([0, len(self.maze[0])-1])







    def __is_path__(self,coordinate):
        y, x = coordinate
        return 0<=x<self.size and 0<=y<self.size and self.maze[y][x]==0

    def __is_wall__(self,coordinate):
        y, x = coordinate
        return 0<=x<self.size and 0<=y<self.size and self.maze[y][x]==1


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = M_G(19)
    demo.show()
    sys.exit(app.exec_())
